package com.hello.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hello.common.log.annotation.Log;
import com.hello.common.log.enums.BusinessType;
import com.hello.common.security.annotation.RequiresPermissions;
import com.hello.system.domain.RentalPayments;
import com.hello.system.service.IRentalPaymentsService;
import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.common.core.utils.poi.ExcelUtil;
import com.hello.common.core.web.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 支付记录Controller
 * 
 * @author hello
 * @date 2025-07-20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/payments")
public class RentalPaymentsController extends BaseController
{
    private final IRentalPaymentsService rentalPaymentsService;

    /**
     * 查询支付记录列表
     */
    @RequiresPermissions("system:payments:list")
    @GetMapping("/list")
    public TableDataInfo list(RentalPayments rentalPayments)
    {
        log.info("======== 查询支付记录列表请求 ========");
        log.info("请求参数: {}", rentalPayments);
        
        startPage();
        List<RentalPayments> list = rentalPaymentsService.selectRentalPaymentsList(rentalPayments);
        TableDataInfo result = getDataTable(list);
        
        log.info("查询结果: 总记录数={}, 当前页数据量={}", result.getTotal(), list.size());
        log.info("======== 查询支付记录列表完成 ========");
        
        return result;
    }

    /**
     * 导出支付记录列表
     */
    @RequiresPermissions("system:payments:export")
    @Log(title = "支付记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RentalPayments rentalPayments)
    {
        log.info("======== 导出支付记录数据请求 ========");
        log.info("导出条件: {}", rentalPayments);
        
        List<RentalPayments> list = rentalPaymentsService.selectRentalPaymentsList(rentalPayments);
        log.info("导出数据量: {} 条记录", list.size());
        
        ExcelUtil<RentalPayments> util = new ExcelUtil<RentalPayments>(RentalPayments.class);
        util.exportExcel(response, list, "支付记录数据");
        
        log.info("======== 支付记录数据导出完成 ========");
    }

    /**
     * 获取支付记录详细信息
     */
    @RequiresPermissions("system:payments:query")
    @GetMapping(value = "/{paymentId}")
    public AjaxResult getInfo(@PathVariable("paymentId") Long paymentId)
    {
        log.info("======== 获取支付记录详情请求 ========");
        log.info("支付ID: {}", paymentId);
        
        RentalPayments payment = rentalPaymentsService.selectRentalPaymentsByPaymentId(paymentId);
        log.info("查询结果: {}", payment != null ? "找到支付记录" : "未找到对应支付记录");
        
        log.info("======== 获取支付记录详情完成 ========");
        return success(payment);
    }

    /**
     * 新增支付记录
     */
    @RequiresPermissions("system:payments:add")
    @Log(title = "支付记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RentalPayments rentalPayments)
    {
        log.info("======== 新增支付记录请求 ========");
        log.info("新增数据: {}", rentalPayments);
        
        int result = rentalPaymentsService.insertRentalPayments(rentalPayments);
        log.info("新增结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 新增支付记录完成 ========");
        return toAjax(result);
    }

    /**
     * 修改支付记录
     */
    @RequiresPermissions("system:payments:edit")
    @Log(title = "支付记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RentalPayments rentalPayments)
    {
        log.info("======== 修改支付记录请求 ========");
        log.info("修改数据: {}", rentalPayments);
        
        int result = rentalPaymentsService.updateRentalPayments(rentalPayments);
        log.info("修改结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 修改支付记录完成 ========");
        return toAjax(result);
    }

    /**
     * 删除支付记录
     */
    @RequiresPermissions("system:payments:remove")
    @Log(title = "支付记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paymentIds}")
    public AjaxResult remove(@PathVariable Long[] paymentIds)
    {
        log.info("======== 删除支付记录请求 ========");
        log.info("删除支付ID列表: {}", java.util.Arrays.toString(paymentIds));
        log.info("删除数量: {} 个支付记录", paymentIds.length);
        
        int result = rentalPaymentsService.deleteRentalPaymentsByPaymentIds(paymentIds);
        log.info("删除结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 删除支付记录完成 ========");
        return toAjax(result);
    }
}
